package cn.me.alphamq.nameserver.task.cluster.masterslave;

import cn.me.alphamq.common.enumeration.EventCodeEnum;
import cn.me.alphamq.common.msg.TcpMsg;
import cn.me.alphamq.nameserver.cache.Container;
import cn.me.alphamq.nameserver.config.NameServerProperties;
import cn.me.alphamq.nameserver.event.model.cluster.StartReplicationReqEvent;
import cn.me.alphamq.nameserver.task.cluster.ReplicationTask;
import com.alibaba.fastjson2.JSON;

/**
 * 主从复制中从节点发送复制请求的任务，此任务不会阻塞，发送复制请求之后等待主节点返回复制响应，返回响应之后启动一个用于定时发送心跳包的线程
 *
 * @author f
 */
public class SlaveStartReplicationTask extends ReplicationTask {
    @Override
    protected void replicationTask() {
        NameServerProperties nameServerProperties = Container.getNameServerProperties();
        // 向服务器发送复制请求
        StartReplicationReqEvent startReplicationReqEvent = new StartReplicationReqEvent();
        startReplicationReqEvent.setUsername(nameServerProperties.getNameServerUsername());
        startReplicationReqEvent.setPassword(nameServerProperties.getNameServerPassword());
        Container.getMultiFunctionalChannel().writeAndFlush(new TcpMsg(EventCodeEnum.START_REPLICATION_REQ,
                JSON.toJSONBytes(startReplicationReqEvent)));
    }
}
